<script lang="ts" setup name="Goods">
import useStore from '@/store';
import { storeToRefs } from 'pinia';
import { onBeforeMount, ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router'
import { CartItem } from '@/types/cart'

import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message';

const route = useRoute()
const { goods, cart } = useStore()
const { info } = storeToRefs(goods)
// 商品数量
const count = ref(1)
// 保存skuId
const currentSkuId = ref('')

// 监听商品id - 商品信息数据获取
watchEffect(() => {
  const id = route.params.id as string
  if (route.fullPath === `/goods/${id}`) {
    goods.getGoodsInfo(id)
  }
})

onBeforeMount(() => {
  goods.resetGoodsInfo()
})


// 渲染商品的信息
const changeSkuFn = (skuId: string) => {
  const sku = info.value.skus.find(item => item.id === skuId)
  // console.log(sku);
  currentSkuId.value = skuId
  if (sku) {
    info.value.inventory = sku.inventory
    info.value.price = sku.price
    info.value.oldPrice = sku.oldPrice
  }
}

// 加入购物车
const addCart = async () => {
  // 判断sku是否是完整(是否全选)的
  if (!currentSkuId.value) {
    return Message.warning('请选择商品的完整信息')
  }
  console.log(info.value);
  // 1. 确定当前添加的商品的sku
  const sku = info.value.skus.find(item => item.id === currentSkuId.value)
  // 2. 将 sku规格信息中name + valuename拼接起来
  const attrsText = sku?.specs.map(item => item.name + ':' + item.valueName).join(' ')
  // console.log(attrsText);


  cart.addCart({
    // 本地添加
    id: info.value.id,  // 商品id 
    name: info.value.name, // 商品名称
    picture: info.value.mainPictures[0], // 商品图片(封面)
    price: info.value.price,  // 商品价格
    count: count.value,   // 商品数量
    skuId: currentSkuId.value, // 规格 skuid
    attrsText: attrsText,   // 规格描述 -> 蓝色 ...
    selected: true,  // 是否选中 
    nowPrice: info.value.price, // 商品现价
    stock: info.value.inventory, // 商品库存
    isEffective: true,  // 是否有效
  } as CartItem)

  Message.success('加入购物车成功')
}

</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <template v-if="info.categories">
        <!-- 面包屑 -->
        <XtxBread>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem :to="`/category/${info.categories[1].id}`">{{ info.categories[1].name }}</XtxBreadItem>
          <XtxBreadItem :to="`/category/sub/${info.categories[0].id}`">{{ info.categories[0].name }}</XtxBreadItem>
          <XtxBreadItem to="/">{{ info.name }}</XtxBreadItem>
        </XtxBread>
        <!-- 商品信息 -->
        <div class="goods-info">
          <div class="media">
            <!-- 商品图片显示组件 -->
            <GoodsImage :images="info.mainPictures"></GoodsImage>
            <!-- 商品销量 -->
            <GoodsSales></GoodsSales>
          </div>
          <div class="spec">
            <!-- **商品信息 (商品名称, 描述, 价格, 城市选择...) -->
            <GoodsName :goods="info"></GoodsName>
            <!-- 商品的规格信息  skuId="1369155864430120962" -->
            <GoodsSku :goods="info" @changeSku="changeSkuFn"></GoodsSku>
            <!-- 数字选择框 -->
            <XtxNumbox :max="info.inventory" showLabel v-model="count"></XtxNumbox>
            <XtxButton type="primary" @click="addCart" style="margin-top: 20px">
              加入购物车
            </XtxButton>
          </div>
        </div>
        <!-- 商品详情 -->
        <div class="goods-footer">
          <div class="goods-article">
            <!-- 商品+评价 -->
            <div class="goods-tabs">
              <!-- 商品详情 -->
              <GoodsDetail :goods="info" />
            </div>
          </div>
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside">
            <GoodsHot :type="1" />
            <GoodsHot :type="2" />
            <GoodsHot :type="3" />
          </div>
        </div>
      </template>


    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}


.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>